<?php
/**
 *
 *
 */
class Yz_Action_DeleteItem
    extends Yz_Action_Delete
{

    /**
     *  The item to Delete
     *  @var Item
     */
    protected $item;


    /************************************
     *          Public methods          *
     ************************************/

    /**
     *  @param mixed $item
     *  @return this
     */
    public function setItem(Yz_Item_Interface $item)
    {
        if (! $item->id)
            throw ValueError::_new_();
        $this->item = $item;
        // prevent the action instance from returning results after changing parameters
        $this->executed = false;
        return $this;
    }

    /************************************
     *        Protected methods         *
     ************************************/

    /** Verify arguments before building query
     *      prevent deletion without sensible arguments
     *  @return this
     */
    protected function verifyArguments()
    {
        debug();
        if (! $this->item)
            // assert that if values are non-empty, the fields have been set, too (@see identifyValues)
            throw StateError::_new_();
        return $this;
    }


    /**
     *  Common UPDATE statement creator:
     *      the common part of statement
     *  @return Yz_SQL_Statement_Update
     */
    protected function buildStatement()
    {
        debug();
        return parent::buildStatement()
            ->where(key($this->tables) . ".id=:id")
        ;
    }

    /**
     *  Apply the arguments to the SQL statement
     *      Bind the id argument
     *  @param Yz_SQL_Statement $sql
     *  @return this
     */
    protected function applyArguments(Yz_SQL_Statement $sql)
    {
        debug();
        $sql
            ->bind("id", $this->item->id)
        ;
        return $this;
    }

}
